package com.itheima.web.service.impl.oms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.oms.OrderDeliveryDto;
import com.itheima.dto.oms.OrderReceiverDto;
import com.itheima.result.PageResult;
import com.itheima.service.oms.OmsOrderOperateHistoryService;
import com.itheima.service.oms.OmsOrderService;
import com.itheima.utils.StringUtils;
import com.itheima.vo.oms.OmsOrder;
import com.itheima.vo.oms.OmsOrderItem;
import com.itheima.vo.oms.OmsOrderOperateHistory;
import com.itheima.vo.oms.OrderDetailsVo;
import com.itheima.web.mapper.oms.OmsOrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;


@DubboService
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {
    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OmsOrderItemServiceImpl omsOrderItemService;
    @Autowired
    private OmsOrderService omsOrderService;
    @Autowired
    private OmsOrderOperateHistoryService omsOrderOperateHistoryService;
    //根据订单id查询订单详情
    @Override
    public OrderDetailsVo findById(Integer id) {
        long orderId = id.longValue();
        OmsOrder omsOrder = omsOrderMapper.selectById(orderId);
        List<OmsOrderItem> orderItem = omsOrderItemService.findOrderItem(orderId);
        List<OmsOrderOperateHistory> list = omsOrderOperateHistoryService.findOperateHistory(orderId);
        return OrderDetailsVo.init(omsOrder,list,orderItem);
    }
    //备注订单
    @Override
    public Integer note(Long id, Integer status, String note) {
        //封装一个实体类history把修改后信息放入并保存
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(id);
        history.setNote(note);
        history.setOrderStatus(status);
        history.setCreateTime(LocalDateTime.now());
      history.setOperateMan("后台管理员");
      return omsOrderOperateHistoryService.saveOne(history);


    }
    //分页查询订单详情
    @Override
    public PageResult listPage(String createTime, String orderSn, String receiverKeyword, Integer orderType, Integer pageNum, Integer pageSize, Integer sourceType, Integer status) {
        Page<OmsOrder> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(createTime != null,OmsOrder::getCreateTime,createTime)
                .eq(orderType != null,OmsOrder::getOrderType,orderType)
                .eq(sourceType != null,OmsOrder::getSourceType,sourceType)
                .eq(status != null,OmsOrder::getStatus,status)
                .like(orderSn != null,OmsOrder::getOrderSn,orderSn)
                .orderByAsc(OmsOrder::getId);
        if (StringUtils.startsWith(receiverKeyword, "1")) {
           lqw.like(receiverKeyword!=null,OmsOrder::getReceiverPhone,receiverKeyword);
        } else {
         lqw.like(receiverKeyword!=null,OmsOrder::getReceiverName,receiverKeyword);
        }
        this.page(pageInfo,lqw);
        PageResult result = PageResult.init(pageInfo.getTotal(), pageNum.longValue(), pageSize.longValue(), pageInfo.getRecords());
        return result;
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     */
    @Override
    public void closeOrders(List<Integer> ids, String note) {
        List<OmsOrder> omsOrders = omsOrderMapper.selectBatchIds(ids);
        for (OmsOrder omsOrder : omsOrders) {
            omsOrder.setStatus(4);
            omsOrder.setNote(note);
            omsOrderService.updateById(omsOrder);
        }
    }

    /**
     * 批量发货
     * @param list
     */
    @Override
    public void delivery(List<OrderDeliveryDto> list) {
      for (OrderDeliveryDto orderDeliveryDto : list) {
          Integer id = orderDeliveryDto.getOrderId();
          OmsOrder omsOrder = this.getById(id);
          omsOrder.setDeliverySn(orderDeliveryDto.getDeliverySn());
          omsOrder.setDeliveryCompany(orderDeliveryDto.getDeliveryCompany());
          omsOrder.setStatus(2);
          omsOrder.setDeliveryTime(LocalDateTime.now());
          omsOrder.setModifyTime(LocalDateTime.now());
        this.updateById(omsOrder);
    }
    for (OrderDeliveryDto orderDeliveryDto : list) {
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        Integer id = orderDeliveryDto.getOrderId();
            history.setOrderId(id.longValue());
            history.setOrderStatus(2);
            history.setOperateMan("后台管理员");
            history.setNote("完成发货");
            history.setCreateTime(LocalDateTime.now());
            omsOrderOperateHistoryService.save(history);
    }
    }

    /**
     * 修改收货人信息
     * @param dto
     */
    @Override
    public void updateReceiver(OrderReceiverDto dto) {
        Integer id = dto.getOrderId();
        OmsOrder omsOrder = this.getById(id);
        BeanUtils.copyProperties(dto,omsOrder);
        this.updateById(omsOrder);
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setCreateTime(LocalDateTime.now());
        history.setOrderId(id.longValue());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(dto.getStatus());
        history.setNote("修改收货人信息");
        omsOrderOperateHistoryService.save(history);
    }


}
